export class UI {
  private crosshair!: HTMLElement;
  private canvas: HTMLCanvasElement;
  private instructions!: HTMLElement;

  constructor(canvas: HTMLCanvasElement) {
    this.canvas = canvas;
    this.createCrosshair();
    this.createInstructions();
    this.hideCursor();
  }

  private createInstructions(): void {
    this.instructions = document.createElement('div');
    this.instructions.id = 'instructions';
    this.instructions.style.cssText = `
      position: fixed;
      top: 20px;
      left: 20px;
      color: white;
      font-family: Arial, sans-serif;
      font-size: 14px;
      background: rgba(0, 0, 0, 0.7);
      padding: 15px;
      border-radius: 8px;
      z-index: 1000;
      line-height: 1.5;
      pointer-events: none;
    `;
    
    this.instructions.innerHTML = `
      <div style="font-size: 16px; font-weight: bold; margin-bottom: 10px;">🎮 我的世界网页版</div>
      <div><strong>点击屏幕</strong> - 锁定鼠标</div>
      <div><strong>WASD</strong> - 移动</div>
      <div><strong>空格</strong> - 跳跃</div>
      <div><strong>鼠标</strong> - 转动视角</div>
      <div><strong>左键</strong> - 破坏方块</div>
      <div><strong>右键</strong> - 放置方块</div>
      <div><strong>1-9</strong> - 切换物品</div>
      <div style="margin-top: 10px; font-size: 12px; opacity: 0.8;">
        按 ESC 解锁鼠标
      </div>
    `;
    
    document.body.appendChild(this.instructions);
    
    // 鼠标锁定时隐藏说明
    document.addEventListener('pointerlockchange', () => {
      if (document.pointerLockElement === this.canvas) {
        this.instructions.style.display = 'none';
      } else {
        this.instructions.style.display = 'block';
      }
    });
  }

  private createCrosshair(): void {
    this.crosshair = document.createElement('div');
    this.crosshair.id = 'crosshair';
    this.crosshair.style.cssText = `
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 20px;
      height: 20px;
      pointer-events: none;
      z-index: 1000;
    `;
    
    // 创建十字准星
    this.crosshair.innerHTML = `
      <div style="
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 2px;
        height: 16px;
        background: white;
        box-shadow: 0 0 2px rgba(0,0,0,0.8);
      "></div>
      <div style="
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 16px;
        height: 2px;
        background: white;
        box-shadow: 0 0 2px rgba(0,0,0,0.8);
      "></div>
    `;
    
    document.body.appendChild(this.crosshair);
  }

  private hideCursor(): void {
    this.canvas.style.cursor = 'none';
    
    // 当指针锁定时隐藏准星外的光标
    document.addEventListener('pointerlockchange', () => {
      if (document.pointerLockElement === this.canvas) {
        document.body.style.cursor = 'none';
      } else {
        document.body.style.cursor = 'auto';
      }
    });
  }

  public showCrosshair(): void {
    this.crosshair.style.display = 'block';
  }

  public hideCrosshair(): void {
    this.crosshair.style.display = 'none';
  }
}